// pages/actualPage/scheduleCard/index.js
import {
  getCurrWeekList,
  formateDate
} from '../../../utils/tools'

const db = wx.cloud.database()
const kong = {
  //第一节
  one: [{
      sub: '',
      add: '',
      tec: "",
      color: '',
      type: 1, //0-无  1-有
    },
    {
      sub: '',
      add: '',
      tec: "",
      color: '',
      type: 0,
    }, {
      sub: '',
      add: '',
      tec: "",
      color: '',
      type: 1,
    },
    {
      sub: '',
      add: '',
      tec: "",
      color: '',
      type: 0,
    },
    {
      sub: '',
      add: '',
      tec: "",
      color: '',
      type: 0,
    },
  ],
  //第二节
  two: [{
      sub: '',
      add: '',
      tec: "",
      color: '',
      type: 0,
    },
    {
      sub: '',
      add: '',
      tec: "",
      color: '',
      type: 1, //0-无  1-有
    },
    {
      sub: '',
      add: '',
      tec: "",
      color: '',
      type: 1, //0-无  1-有
    }, {
      sub: '',
      add: '',
      tec: "",
      color: '',
      type: 0,
    },
    {
      sub: '',
      add: '',
      tec: "",
      color: '',
      type: 1, //0-无  1-有
    },
  ],
  //第三节
  three: [{
      sub: '',
      add: '',
      tec: "",
      color: '',
      type: 0,
    },
    {
      sub: '',
      add: '',
      tec: "",
      color: '',
      type: 0,
    },
    {
      sub: '',
      add: '',
      tec: "",
      color: '#',
      type: 1, //0-无  1-有
    },
    {
      sub: '',
      add: '',
      tec: "",
      color: '',
      type: 1, //0-无  1-有
    },
    {
      sub: '',
      add: '',
      tec: "",
      color: '',
      type: 0,
    },
  ],
  //第四节
  four: [{
      sub: '',
      add: '',
      tec: "",
      color: '',
      type: 1, //0-无  1-有
    }, {
      sub: '',
      add: '',
      tec: "",
      color: '',
      type: 1, //0-无  1-有
    }, {
      sub: '',
      add: '',
      tec: "",
      color: '',
      type: 0,
    },
    {
      sub: '',
      add: '',
      tec: "",
      color: '',
      type: 0,
    },
    {
      sub: '',
      add: '',
      tec: "",
      color: '',
      type: 0,
    },
  ]
}
let jieliu = null

Page({
  data: {
    showweekk: false, //显示选择
    xzweek: "",
    currentWeek: 2,
    time: {
      /*
        9:00-10:00第一节课
        10:00-11:40第二节课

        13:20-17:50第三节课

        18:20-21:35第三节课
      
      */
      //上午
      one: [{
          index: 1,
          timeStart: '09:00',
          timeEnd: '10:00'
        },
        {
          index: 2,
          timeStart: '10:00',
          timeEnd: '11:40'
        },
        // {
        //   index: 3,
        //   timeStart: '09:50',
        //   timeEnd: '10:45'
        // },
        // {
        //   index: 4,
        //   timeStart: '10:50',
        //   timeEnd: '11:35'
        // }
      ],
      //下午
      two: [{
          index: 3,
          timeStart: '13:20',
          timeEnd: '17:50'
        },
        // {
        //   index: 6,
        //   timeStart: '14:25',
        //   timeEnd: '15:10'
        // },
      ],
      //晚上
      three: [{
          index: 4,
          timeStart: '18:20',
          timeEnd: '21:35'
        },
        //   {
        //     index: 8,
        //     timeStart: '16:15',
        //     timeEnd: '17:00'
        //   },
      ]
    },
    color: ["#98c369", "#a1e8e4", "#b4abc9", "#a4c8fc", "#a7e3e3", "#faf1b9", "#add3bb", "#a8bcd9", "#f2c6a5", "#f4b7b6", "#baefe9", "#e9b5cb", "#a6d5f3", "#c9b0cc", "#a7d5d5", "#a9b0cd", "#98c369", "#a1e8e4", "#b4abc9", "#a4c8fc"],
    //课程信息
    schedule: {
      //第一节
      one: [{
          sub: '',
          add: '',
          tec: "",
          color: '',
          type: 1, //0-无  1-有
        },
        {
          sub: '',
          add: '',
          tec: "",
          color: '',
          type: 0,
        }, {
          sub: '',
          add: '',
          tec: "",
          color: '',
          type: 1,
        },
        {
          sub: '',
          add: '',
          tec: "",
          color: '',
          type: 0,
        },
        {
          sub: '',
          add: '',
          tec: "",
          color: '',
          type: 0,
        },
      ],
      //第二节
      two: [{
          sub: '',
          add: '',
          tec: "",
          color: '',
          type: 0,
        },
        {
          sub: '',
          add: '',
          tec: "",
          color: '',
          type: 1, //0-无  1-有
        },
        {
          sub: '',
          add: '',
          tec: "",
          color: '',
          type: 1, //0-无  1-有
        }, {
          sub: '',
          add: '',
          tec: "",
          color: '',
          type: 0,
        },
        {
          sub: '',
          add: '',
          tec: "",
          color: '',
          type: 1, //0-无  1-有
        },
      ],
      //第三节
      three: [{
          sub: '',
          add: '',
          tec: "",
          color: '',
          type: 0,
        },
        {
          sub: '',
          add: '',
          tec: "",
          color: '',
          type: 0,
        },
        {
          sub: '',
          add: '',
          tec: "",
          color: '#',
          type: 1, //0-无  1-有
        },
        {
          sub: '',
          add: '',
          tec: "",
          color: '',
          type: 1, //0-无  1-有
        },
        {
          sub: '',
          add: '',
          tec: "",
          color: '',
          type: 0,
        },
      ],
      //第四节
      four: [{
          sub: '',
          add: '',
          tec: "",
          color: '',
          type: 1, //0-无  1-有
        }, {
          sub: '',
          add: '',
          tec: "",
          color: '',
          type: 1, //0-无  1-有
        }, {
          sub: '',
          add: '',
          tec: "",
          color: '',
          type: 0,
        },
        {
          sub: '',
          add: '',
          tec: "",
          color: '',
          type: 0,
        },
        {
          sub: '',
          add: '',
          tec: "",
          color: '',
          type: 0,
        },
      ]
    },
    weekList: [], //星期数组
    isShow: false, //课程信息开关
    current: {},
    valueweek: [], //默认选择当前周
  },
  getDetail(e) {
    let {
      item
    } = e.currentTarget.dataset;
    // console.log(item)
    this.setData({
      current: item,
      isShow: true
    })
  },
  close() {
    this.setData({
      isShow: false
    })
  },
  async onShow() {
    let time = new Date(),
      list = getCurrWeekList(time),
      weekList = []
    list.forEach(item => {
      weekList.push({
        day: [item.split('-')[1], item.split('-')[2]].join('-'),
        week: "星期" + "日一二三四五六".charAt((new Date(item)).getDay()),
        isCurr: formateDate(time) == item
      })
    });
    //获取当前周数
    let week = this.getweek()
    week = (week == "1" ? "2" : week)
    let da = this.data.schedule
    let kencheng = await this.setKeBiao(week, da).then(res => {
      return res
    })
    // console.log("当前是第"+week+"周");
    this.setData({
      weekList,
      schedule: kencheng,
      currentWeek: week,
      valueweek: [week - 2]
    })
  },
  //选择第几周
  async qieHuanWeek(e) {
    //节流操作
    if (!jieliu) {
      jieliu = new Date()
      let f = this.data.showweekk
      // console.log(e.target.dataset.key);
      if (f && e.target.dataset.key == 1) {
        //显示 并且点击确认
        let xz = (this.data.xzweek + 1)
        //查询数据库并渲染
        let da = this.data.schedule
        // console.log("选择第几周"+xz);
        // debugger
        let sd = await this.setKeBiao(xz, da).then(res => {
          return res
        })
        // console.log("设置的数据");
        // console.log(sd);
        if (sd) {
          this.setData({
            schedule: sd,
            currentWeek: xz,
          })
        } else {
          this.setData({
            schedule: kong,
            currentWeek: xz,
          })
        }
      } else {
      }
      this.setData({
        showweekk: !f
      })
      setTimeout(()=>{
        jieliu=null
      },1000)
    }
  },
  //滑动结束
  bindChange(e) {
    const val = e.detail.value[0] + 1
    //获取选择的周数
    // console.log(val);
    this.setData({
      xzweek: val
    })

  },

  //设置课表数据
  async setKeBiao(week, da) {
    week = (week == "1" ? "2" : week)
    // 查询数据库信息
    let d = await this.getDate(week).then(res => {
      return res[0]
    })
    // console.log("第"+week+"周数据");
    // console.log(d)
    // console.log("______________________");
    // console.log("传入的数据");
    // console.log(da);
    // console.log("________________________");
    if (d) {
      let kecheng = da
      // console.log(kecheng);
      // 渲染数据
      try {
        kecheng.one.forEach((item, index) => {
          // 第一节课
          this.updateCourseInfo(item, d.one.kc[index], this.data.color, index, 0);
          // 第二节课
          this.updateCourseInfo(kecheng.two[index], d.tow.kc[index], this.data.color, index, 5);
          // 第三节课
          this.updateCourseInfo(kecheng.three[index], d.three.kc[index], this.data.color, index, 10);
          // 第四节课
          this.updateCourseInfo(kecheng.four[index], d.four.kc[index], this.data.color, index, 10);
        });
      } catch (error) {
        console.log("出错" + error);
        return kong
      }
      // console.log(kecheng);
      // console.log(kong)
      // debugger;
      return kecheng
    } else {
      // console.log("没有数据");
      wx.showToast({
        title: '还没有数据',
        icon: 'none',
        duration: 2000
      })

    }
  },
  // 处理每节课的信息更新
  updateCourseInfo(course, courseData, color, index, colorOffset) {
    const colorIndex = index + colorOffset;
    course.sub = courseData;
    course.color = courseData === " " ? '' : color[colorIndex] || '';
    course.add = "";
    course.tec = "";
    course.type = courseData === "" ? '0' : 1;
  },

  //请求数据库信息  根据指定周获取课表数据
  async getDate(week) {
    console.log("=========>" + week);
    week = week + ""
    let sk = await db.collection("kechengbiao").where({
      week: week
    }).get()
    // console.log(sk);
    //没数据就返回0
    // if(sk.data.length==0){
    //   return 0;
    // }
    return sk.data
  },

  //获取指定周的数据
  getweek() {
    //从2月24到现在是第几周
    // 定义开始日期
    const startDate = new Date(2025, 1, 24); // 注意：月份从 0 开始计数，2 月对应 1
    // 获取当前日期
    const currentDate = new Date();
    // 计算两个日期之间的时间差（毫秒）
    const timeDifference = currentDate - startDate;
    // 将时间差转换为天数
    const daysDifference = Math.floor(timeDifference / (1000 * 60 * 60 * 24));
    // 计算周数
    const weeks = Math.ceil(daysDifference / 7) + "";
    console.log(`从 2025 年 2 月 24 日到现在是第 ${weeks} 周`);
    return weeks
  },
})